package com.tamartech.greendb.importers;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.Ostermiller.util.CSVParser;
import com.tamartech.greendb.loader.VotingInfoLoader;

public class VotingInfoImporter
{
    private static Logger logger = Logger.getLogger (VotingInfoImporter.class);
    private static SimpleDateFormat formatDate = new SimpleDateFormat ("dd/MM/yyyy");
        
    public static void main(String[] args) throws Exception
	{
        File inFile = new File (args[0]);
        String wardName = args[1];
        Date rollDate = formatDate.parse(args[2]);
        Date electionDate = formatDate.parse(args[3]);
        String electionType = args[4];
        String voteType = args[5]; // Voted/Postal
        String source = args[6]; // MarkedReg/PollingStn
        String pollingDistrict = args[7]; // KA, KB etc
        
        
        VotingInfoLoader loader = new VotingInfoLoader();
        loader.begin(wardName, rollDate, electionDate, electionType, source);
        
        CSVParser csv = new CSVParser (new FileInputStream (inFile));
        
        
        for (;;)
        {
            // not all cols are labelled
            String[] line = csv.getLine();

            System.out.println("line: " + line);
            
            if (line == null)
                break;
            
            int voterNumber = Integer.valueOf(line[0].trim());
            
            String voterNumberSuffixStr = line[1].trim();
            
            Integer voterNumberSuffix = null;
            
            if (voterNumberSuffixStr.length() > 0)
                voterNumberSuffix = Integer.valueOf(voterNumberSuffixStr);
            
            
            loader.putVoted(pollingDistrict, voterNumber, voterNumberSuffix, voteType);
        }
        
        csv.close();

        loader.end();
	}	
    
    
} 